import http from "@/common/http.js"
import user from "@/common/user.js"
import tool from "@/common/tool.js"

// 查询银行列表
function getBankLists(call) {
	http.request({
		url: '/api/v1/getBankList',
		method: 'POST',
		type: 'json',
		success: ret => {
			call(ret);
		}
	})
}

/**
 * POST /api/v1/getBankInfoList用户银行卡记录接口
 * @param {
	 startDate: 2022-08-15
	 endDate: 2022-08-15
	 current: 1
 } params 
 * @param {*} call 
 */
function getBankInfoList(params, call) {
	params['userId'] = user.getUserId();
	// params['boundStatus'] = '';
	params['size'] = 15;

	user.getSign(secret => {
		params['secret'] = secret;
		console.log(params)
		http.request({
			url: '/api/v1/getBankInfoList',
			data: params,
			success: ret => {
				call(ret);
			},
			fail: ret => {
				console.log(ret);
				uni.showToast({
					icon: 'error',
					title: '获取银行卡信息失败'
				})
			}
		})

	})
}

function checkField(formData, feild, msg) {
	if (!formData[feild]) {
		uni.showToast({
			icon: "error",
			title: msg
		})
		return false;
	}
	return true;
}

// 银行卡4要素验证
function bankVerifySign(formData) {


	if (!checkField(formData, 'account', '请输入银行卡号')) return;
	if (!checkField(formData, 'idNo', '请输入身份证号码')) return;
	if (!checkField(formData, 'mobile', '请输入手机号码')) return;
	if (!checkField(formData, 'realName', '请输入姓名')) return;

	let data = {
		"cardNo": formData.account,
		"idCard": formData.idNo,
		"mobile": formData.mobile,
		"name": formData.realName,
		"userId": user.getUserId()
	}


	http.request({
		url: '/api/v1/bankVerifySign',
		data: data,
		success: ret => {
			addBank(formData);
		},
		fail: ret => {
			uni.showModal({
				title: '银行卡验证失败',
				content: '请核对后再次尝试',
				showCancel: false,
			})
		},
	})
}
// 添加银行卡
function addBank(data) {
	console.log(data)
	http.request({
		url: '/api/v1/addBank',
		method: 'POST',
		data: data,
		success: ret => {
			console.log(ret);
			uni.showModal({
				showCancel:false,
				title:'添加银行卡成功',
				success() {
					if(data.sourcePage){
						uni.switchTab({
							url:'/pages/index/my'
						})
					}else{
						uni.navigateBack()
					}
					
				}
			})
			
			
			
		},
		fail: ret => {
			console.log(ret);
			uni.showModal({
			    title: '银行卡验证失败',
			    content: '请核对后再次尝试',
			    showCancel: false,
			   })
		},
	})
}


// 添加银行卡
function setDeault(id,call) {
	http.request({
		url: '/api/v1/setDefaultBank',
		method: 'POST',
		data: {
			 "id": id,
			  "userId": user.getUserId()
		},
		success: ret => {
			console.log(ret);
			uni.showToast({
				icon:'success',
				title:'设置默认成功',
			})
			call()
		},
		fail: ret => {
			console.log(ret);
			uni.showToast({
				icon:'error',
				title:'设置失败'
			})
		},
	})
}




export default {
	setDeault: setDeault,
	getBankLists: getBankLists,
	addBank: bankVerifySign,
	getBankInfoList: getBankInfoList,
}
